// pages/tobindeleme/tobindeleme.js
const app = getApp();
import httpUtil from '../../utils/httpUtil'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: 0,
    userName:'',
    password:'',
    mobile:'',
    vcode:'',
    qrcode:null,
    needCode: false,
    loginType :1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

 


    // var that = this
    // httpUtil.getRequest('/api/store/loginByQrCode',{

    // }).then(res =>{
    //     console.log(res.url)
    //     that.setData({
    //         qrcode:res.url
    //     })
    // })
    var that = this;
    new Promise((resolve,reject) =>{
      var token = getApp().globalData.token
      console.log(token)
      if(token != null && token != ''){
        resolve(token)
      }else{
        app.isLoginWatch(function(e){
            console.log("get login callback...")
            resolve(e)
          });
      }
    }).then(res =>{
        var storeId = options.storeId;
        if(storeId != null && storeId != ''){
            httpUtil.getRequest('/api/store/getStoreInfo',{
                'storeId':storeId
              }).then(res =>{
                console.log('jinlailema'+res.username)
                that.setData({
                  userName:res.username,
                  password:res.password,
                  mobile:res.mobile,
                })
              })
        }else{
            console.log("storeIdWEIKONG")
        }

    })

  },

  loginbypassword(){
    var username = this.data.userName
    console.log(username)
    if(username == ''){
      wx.showToast({
        title: '请输入用户名',
        icon:'none'
      })
      return;
    }
    var passowrd = this.data.password
    if(passowrd == ''){
      wx.showToast({
        title: '请输入密码',
        icon:'none'
      })
      return;
    }
    console.log('用户名为'+username+'密码为'+passowrd)
    httpUtil.getRequest('/api/store/elemePassword',{
        'username':username,
        'password':passowrd
    }).then(res =>{
        console.log(res)
        if(res.code == 0 || res.code == '0'){
            wx.showToast({
                title: '登录成功',
              })
            wx.reLaunch({
                url: '/pages/index/index'
              })
              return;
        }
        if(res.code == 2 || res.code == '2'){
            this.setData({
                needCode:true,
                loginType:2
            })
            this.setData({
                countdown: 60
              });
              const timer = setInterval(() => {
                if (this.data.countdown > 0) {
                  this.setData({
                    countdown: this.data.countdown - 1
                  });
                } else {
                  clearInterval(timer);
                }
              }, 1000);
              return ;
        }
        wx.showToast({
            title: res.message,
        })


    })


} ,
loginbycode(){
    
    var vcode = this.data.vcode
    var that = this;
    console.log(vcode)
    if(vcode == ''){
      wx.showToast({
        title: '请输入验证码',
        icon:'none'
      })
      return;
    }
    console.log('验证码码为'+vcode)
    httpUtil.getRequest('/api/store/elemepasswordSendCode',{
        'code':vcode
    }).then(res=>{
        if(res.code==0||res.code=='0'){
            wx.showToast({
                title: '登录成功',
              })
            wx.reLaunch({
                url: '/pages/index/index'
              })
              return;
        }
        that.setData({
            countdown: 0,
            needCode:false,
            loginType:1
        });
        wx.showToast({
            title: res.message,
        })


    })
},
  usernameInput:function(e){

    this.setData({
      userName: e.detail.value
    })
    console.log(this.data.userName)
  },
  passwordInput(e){
    this.setData({
      password: e.detail.value
    })
    console.log(this.data.password)
  },
  mobileInput(e){
    this.setData({
      mobile: e.detail.value
    })
    console.log(this.data.mobile)
  },
  vcodeInput(e){
    this.setData({
      vcode: e.detail.value
    })
    console.log('yazhema'+this.data.vcode)
  },
  cookieInput(e){
    this.setData({
      cookie: e.detail.value
    })
    console.log(this.data.cookie)
  },
  sendCode() {
    var mobile = this.data.mobile
    if(mobile == ''){
      wx.showToast({
        title: '请输入手机号',
        icon:'none'
      })
      return;
    }
    console.log('手机号为'+mobile)
    httpUtil.getRequest('/api/store/elemePhoneLogin',{
        'moible':mobile
    }).then(res =>{
        this.setData({
            countdown: 60,
            needCode:true
          });
          const timer = setInterval(() => {
            if (this.data.countdown > 0) {
              this.setData({
                countdown: this.data.countdown - 1
              });
            } else {
              clearInterval(timer);
            }
          }, 1000);
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})